package com.citcc.api.controller;

import com.citcc.api.config.Authorization;
import com.citcc.business.bean.CdzAppUser;
import com.citcc.business.bean.CdzPush;
import com.citcc.business.service.CdzAppUserService;
import com.citcc.business.service.CdzPushService;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.jpush.PushUtil;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.StringUtils;
import com.citcc.framework.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;


@Api(tags = "推送相关")
@RestController
public class PushController extends BaseController {
    @Autowired
    private CdzPushService cdzPushService;
    @Autowired
    private CdzAppUserService cdzAppUserService;
    private Logger logger = LoggerFactory.getLogger(ScanCodeController.class);


    @ApiOperation("推送列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query")
    })
    @RequestMapping(value = "/v1/api/push/list", method = RequestMethod.POST)
    public AjaxResult getList(@ApiIgnore @Authorization CdzAppUser user) {
        try{
            startPage();
            List<CdzPush> list = cdzPushService.getList(user.getId());
            TableDataInfo dataTable = getDataTable(list);
            return success(200,dataTable);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1, "获取列表失败");
        }
    }

    @ApiOperation("删除推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "推送id", paramType = "query"),
    })
    @RequestMapping(value = "/v1/api/push/delete", method = RequestMethod.POST)
    public AjaxResult getStakeList(@RequestParam String id) {
        int result = cdzPushService.delete(id);
        if (result > 0){
            return success(200);
        }else{
            return error(-1,"删除失败");
        }
    }

    @ApiOperation("接收app用户设备id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "设备类型0-Android 1-ios", paramType = "query")
    })
    @PostMapping("/v1/api/updateDevice")
    public AjaxResult updateDevice(@ApiIgnore @Authorization CdzAppUser user,@RequestParam String deviceId, @RequestParam String type) {
        logger.info("updateDevice_deviceId:"+deviceId+",type:"+type);
        try {
            //如果设备id不为空 更新
            if (!StringUtils.isEmpty(deviceId)){
                user.setDeviceId(deviceId);
                user.setType(type);
                cdzAppUserService.update(user);
            }
            return success(200);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"请求失败");
        }
    }

    @ApiOperation("推送消息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "query"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型 0-Android 1-ios", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台 android ，ios", paramType = "query")
    })
    @GetMapping("/v1/api/getPushMessage")
    public AjaxResult getPushMessage(@RequestParam String deviceId, @RequestParam String type, @RequestParam String deviceType, @RequestParam String platform) {
        List<Object> registrationIdList = new ArrayList<>();
        registrationIdList.add(deviceId);
        String title = "测试推送";
        String body = "测试推送1111111";
        try {
            PushUtil.pushMessage(title, body, "","1",registrationIdList,platform);
            return success(200);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1,"请求失败");
        }
    }

}